<?php
//http://127.0.0.1/plant_sql.php/ins=0?humidity=356&&tm=165748994&&motor=1
//http://127.0.0.1/plant_sql.php/ins=0?get=hist&&pagesize=10&&pagenum=0
// http://192.168.2.242:8080/plant_sql.php/ins=0?get=hist&&pagesize=10&&pagenum=0
// http://192.168.2.242:8080/plant_sql.php/ins=0?get_ext=hist&&pagesize=10&&pagenum=0
// $servername = '127.0.0.1:3306'; // 数据库服务器名称
$servername = 'localhost'; // 数据库服务器名称
$uname = 'root'; // 数据库用户名
$pswd = ''; // 数据库密码
$dbname = 'plant'; // 数据库名称
// 创建数据库连接
$conn = new mysqli($servername, $uname, $pswd,$dbname);

// 检查数据库连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

if (isset($_REQUEST['humidity']) && isset($_REQUEST['tm']) && isset($_REQUEST['motor'])) {
    $tm = time();
    // $tm = $_REQUEST['tm'];
    // echo "时间： $tm<br>";
    // return ;
    $humidity = $_REQUEST['humidity'];
    $motor = $_REQUEST['motor'];
    
    $sql = "INSERT INTO `humidity_data` VALUES (NULL, $humidity, $tm ,  $motor);";
    // echo $sql;
    if ($conn->query($sql) === TRUE) {
        echo "insert ok";
    } else {
        echo "insert failed: " . $conn->error;
    }    
}
elseif(isset($_REQUEST['get'])) {
    $hist=$_REQUEST['get'];
    $pagesize=$_REQUEST['pagesize'];
    $pagenum=$_REQUEST['pagenum'];
    // echo $hist;
    // SELECT * FROM `humidity_data` ORDER BY timestamps DESC LIMIT 10 OFFSET 10;
    $sql = "SELECT * FROM `humidity_data` ORDER BY timestamps DESC LIMIT ". $pagesize ." OFFSET ". $pagenum .";";
    // echo $sql . "<br />";

    $result = mysqli_query($conn, $sql);
    $pagenum_real = 0;
    $his_data = "\"his_hum\":[";
    $his_tm = "\"his_tm\":[";

    // 获取返回值
    if ($result) {
        // 循环遍历结果集中的每一行数据
        while ($row = mysqli_fetch_assoc($result)) {
            // 访问每一行数据中的列值
            $column1Value = $row['humidity'];
            $column2Value = $row['timestamps'];
            $his_data = $his_data . $column1Value . ",";
            $his_tm = $his_tm . $column2Value . ",";
            // 处理每一行数据...
            // 例如，将数据输出到 HTML 页面
            // echo "{" . $column1Value . "," .$column2Value . "},";
            $pagenum_real = $pagenum_real + 1;
        }
        
        $his_data = rtrim($his_data, ',');
        $his_data = $his_data . "]";
        $his_tm = rtrim($his_tm, ',');
        $his_tm = $his_tm . "]";

        echo "{\"pagesize\":$pagesize" . ",";
        echo "\"pagenum\": $pagenum_real" . ",";
        echo  $his_data . "," ;
        echo  $his_tm ;
        echo "}";
        // 释放结果集
        mysqli_free_result($result);
    } else {
    // 处理查询失败的情况
        echo "查询失败：" . mysqli_error($connection);
    }
}
elseif(isset($_REQUEST['get_ext'])) {
    $pagesize=$_REQUEST['pagesize'];
    $pagenum=$_REQUEST['pagenum'];
    // SELECT * FROM `humidity_data` ORDER BY timestamps DESC LIMIT 10 OFFSET 10;
    $sql = "SELECT * FROM `humidity_data` ORDER BY timestamps DESC LIMIT ". $pagesize ." OFFSET ". $pagenum .";";
    // echo $sql . "<br />";

    $result = mysqli_query($conn, $sql);
    $pagenum_real = 0;
    $his_data = "\"his_hum\":[";
    $his_tm = "\"his_tm\":[";
    $his_motor = "\"motor\":[";

    // 获取返回值
    if ($result) {
        // 循环遍历结果集中的每一行数据
        while ($row = mysqli_fetch_assoc($result)) {
            // 访问每一行数据中的列值
            $column1Value = $row['humidity'];
            $column2Value = $row['timestamps'];
            $column3Value = $row['motor'];
            $his_data = $his_data . $column1Value . ",";
            $his_tm = $his_tm . $column2Value . ",";
            $his_motor = $his_motor . $column3Value . ",";
            // 处理每一行数据...
            // 例如，将数据输出到 HTML 页面
            // echo "{" . $column1Value . "," .$column2Value . "},";
            $pagenum_real = $pagenum_real + 1;
        }
        
        $his_data = rtrim($his_data, ',');
        $his_data = $his_data . "]";
        $his_tm = rtrim($his_tm, ',');
        $his_tm = $his_tm . "]";
        $his_motor = rtrim($his_motor, ',');
        $his_motor = $his_motor . "]";

        echo "{\"pagesize\":$pagesize" . ",";
        echo "\"pagenum\": $pagenum_real" . ",";
        echo  $his_data . "," ;
        echo  $his_motor . "," ;
        echo  $his_tm ;
        echo "}";
        // 释放结果集
        mysqli_free_result($result);
    } else {
    // 处理查询失败的情况
        echo "查询失败：" . mysqli_error($connection);
    }
}
else
{
    echo "param err";
}

// 关闭数据库连接
$conn->close();



?>